<template>
  <el-dialog title="维修单选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal=false
             width="80%"
             center
  >
    <el-card style="border-radius: 10px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审批状态" clearable>
          <el-option label="草稿" value="0"/>
          <el-option label="审核中" value="1"/>
          <el-option label="已审核" value="2"/>
          <el-option label="结项" value="3"/>
          <el-option label="已驳回" value="4"/>
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </el-card>
    <el-row :gutter="10" class="mb8">
    </el-row>
    <el-card style="border-radius: 10px">
    <el-table v-loading="loading" :data="projectInfoList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column width="50" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedRepairId" :label="scope.row.id" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="项目编号" align="center" prop="projectCode">
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName"/>
      <el-table-column label="版本" align="center" prop="version"/>
      <el-table-column label="审批状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_info_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customerName"/>
      <el-table-column label="产品编码" align="center" prop="productId"/>
      <el-table-column label="产品名称" align="center" prop="productName"/>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  processList
} from "@/api/mes/wm/rtissueother";
import {genCode} from "@/api/system/autocode/rule";
import ClientSelect from "@/components/clientSelect/single.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
import {treeSelect, listAll} from "@/api/mes/xm/templateInfo";
import ProjectUser from "@/views/mes/xm/projectUser/index.vue";
import ItemInfo from "@/views/mes/xm/itemInfo/index.vue";
import {getPermissions} from "@/api/mes/xm/projectUser";

export default {
  name: "ProjectInfo",
  dicts: ['project_info_status'],
  components: {ProjectUser, ItemSelect, ClientSelect, ItemInfo},
  data() {
    return {
      selectedRows: undefined,
      showFlag:false,
      selectedRepairId: undefined,
      nowTemplateId: "",
      nowTemplateName: "",
      activeName: 'first',
      allDataListOne: [],
      dataOptions: null,
      dataList: [],
      dataListOne: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      editTitle: "",
      editOpen: false,
      fileType: [
        // 文档类
        "pdf", "doc", "docx", "xls", "xlsx",
        // 图片类
        "jpg", "jpeg", "png", "gif", "bmp",
        // 视频类
        "mp4", "avi", "rmvb", "mkv", "mov",
        // 压缩文件类
        "zip", "rar", "7z", "tar", "gz"
      ],
      permissionsList: [],
      showInoutAdd: false,
      fileList: [],
      fileListBg:[],
      optType: null,
      inoptType: null,
      autoGenFlag: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息表格数据
      projectInfoList: [],
      // 弹出层标题
      title: "",
      bgtitle: "",
      bgopen: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null, status: null, customerName: null, productName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectCode: [
          {required: true, message: "项目编号不能为空", trigger: "blur"}
        ], projectName: [
          {required: true, message: "项目名称不能为空", trigger: "blur"}
        ], templateId: [
          {required: true, message: "请选择项目模板", trigger: "blur"}
        ],bgCode:[
          {required: true, message: "变更申请编号不能为空", trigger: "blur"}
        ]
      },

      templateInfoList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目信息列表 */
    getList() {
      this.selectedRows=[]
      this.loading = true;
      processList(this.queryParams).then(response => {
        this.projectInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row){
      if(row){
        this.selectedRows = row;
        this.$emit('onSelected',this.selectedRows);
        this.showFlag = false;
        this.selectedRows=[]
      }
    },
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedRepairId == null || this.selectedRepairId == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
      this.selectedRows=[]
    },

    // 获取图纸分类树
    getTreeSelect(id) {
      treeSelect(id).then(response => {
        this.dataOptions = response.data.tree;
        this.dataList = response.data.list;
        this.findItemById(this.dataList, id);
      })
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.nowTemplateId = data.id;
      this.nowTemplateName = data.label;
      if (this.permissionsList.includes(this.form.templateId)) {
        this.showInoutAdd = true;
      } else {
        this.showInoutAdd = this.permissionsList.includes(this.nowTemplateId);
      }
    },
    //点击编号
    handleView(row) {
      this.reset();
      this.getTemplateInfoList();
      const id = row.id || this.ids
      getProjectInfo(id).then(response => {
        this.form = response.data;
        this.nowTemplateId = this.form.templateId;
        this.nowTemplateName = this.form.templateName;
        this.fileList.push(response.data.fileId);
        this.editOpen = true;
        this.optType = 'view';
        this.inoptType = 'view';
        this.getTreeSelect(this.form.templateId);
        this.editTitle = "项目信息";
        this.showInoutAdd = false;
      });
      /*this.reset();
      this.getTemplateInfoList();
      const id = row.id || this.ids
      getProjectInfo(id).then(response => {
        this.fileList.push(response.data.fileId);
        this.form = response.data;
        this.open = true;
        this.optType = 'view';
        this.title = "查看项目信息";
      });*/
    },
    getPermissions() {
      getPermissions().then(response => {
        this.permissionsList = response.data;
        if (this.permissionsList.includes(this.form.templateId)) {
          this.showInoutAdd = true;
        } else {
          this.showInoutAdd = this.permissionsList.includes(this.nowTemplateId);
          //this.showInoutAdd = false;
        }
      })
    },
    handleChange(val) {
      this.form.templateName = this.templateInfoList.find(item => item.id == val).templateName;
    },
    getTemplateInfoList() {
      listAll().then(response => {
        this.templateInfoList = response.data;
      });
    },
    // 上传成功回调
    handleUploaded(data) {
      this.form.fileId = data;
    },
    // 删除成功回调
    handleRemoved(data) {
      this.form.fileId = data;
    },
    // 上传成功回调
    handleUploadedBg(data) {
      this.form.bgFileId = data;
    },
    // 删除成功回调
    handleRemovedBg(data) {
      this.form.bgFileId = data;
    },
    handleSelectProduct() {
      this.$refs.itemSelect.showFlag = true;
    },
    // 产品物料选择弹出框
    onItemSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.productId = obj.itemCode;
        this.form.productName = obj.itemName;
        this.form.specification = obj.specification;
      }
    },
    //客户选择弹出框
    onClientSelected(obj) {
      if (obj != undefined && obj != null) {
        this.form.customerId = obj.clientId;
        this.form.customerName = obj.clientName;
      }
    },
    handleSelectClient() {
      this.$refs.clientSelect.showFlag = true;
    },
    handleAutoGenChange(autoGenFlag) {
      if (autoGenFlag) {
        genCode('XM_PROJECT_CODE').then(response => {
          this.form.projectCode = response;
        });
      } else {
        this.form.projectCode = null;
      }
    },
    handleAutoGenChangeBg(autoGenFlag) {
      if (autoGenFlag) {
        genCode('XM_CHANGE_CODE').then(response => {
          this.form.bgCode = response;
        });
      } else {
        this.form.bgCode = null;
      }
    },

    // 取消按钮
    cancel() {
      this.selectedRows=[]
      this.showFlag = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectCode: null,
        projectName: null,
        version: null,
        status: 0,
        customerId: null,
        customerName: null,
        productId: null,
        productName: null,
        templateId: null,
        templateName: null,
        specification: null,
        plannedStartDate: null,
        plannedEndDate: null,
        applicant: null,
        applicationDate: null,
        fileId: null,
        bgReason: null,
        bgFileId: null,
        bgCode: null,
        bgOldId: null,
        bgStatus: 0,
        bgDate: null,
        remarks: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        tenantId: null
      };
      this.fileListBg = [];
      this.fileList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTemplateInfoList();
      this.optType = 'add';
      this.handleAutoGenChange(true);
      this.open = true;
      this.title = "添加项目信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTemplateInfoList();
      const id = row.id || this.ids
      getProjectInfo(id).then(response => {
        this.fileList.push(response.data.fileId);
        this.form = response.data;
        this.open = true;
        this.optType = 'update';
        this.title = "修改项目信息";
      });
    },







  }
};
</script>
